<template>
  <!-- 侧边栏 -->
  <div class="sidebar" :class="{ open: sidebarOpen }">
    <!-- 系统图标及名称 -->
    <div class="hospital-brand">
      <div class="hospital-icon">
        <img :src="logoUrl" alt="" />
      </div>
      <div class="brand-text">智医云联</div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <router-link
        v-for="item in flatMenuItems"
        :to="item.path"
        :key="item.key"
        custom
        v-slot="{ navigate, isActive }"
      >
        <div class="nav-item" :class="{ active: isActive }" @click="navigate">
          <div class="nav-icon">
            <el-icon><component :is="item.icon" /></el-icon>
          </div>
          <div class="nav-text">{{ item.name }}</div>
          <div class="nav-badge" v-if="item.badge && item.badge > 0">
            {{ item.badge }}
          </div>
        </div>
      </router-link>

      <!-- 患者病历管理折叠菜单 -->
      <!-- <div
        class="nav-item collapsible"
        :class="{ active: isPatientRecordActive }"
        @click="togglePatientRecordMenu"
      >
        <div class="nav-icon">
          <el-icon><UserFilled /></el-icon>
        </div>
        <div class="nav-text">患者病历管理</div>
        <div class="nav-arrow" :class="{ rotated: patientRecordMenuOpen }">
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div> -->

      <!-- 患者病历管理子菜单 -->
      <!-- <div class="sub-menu" :class="{ open: patientRecordMenuOpen }">
        <router-link
          v-for="item in patientRecordSubItems"
          :to="item.path"
          :key="item.key"
          custom
          v-slot="{ navigate, isActive }"
        >
          <div
            class="sub-nav-item"
            :class="{ active: isActive }"
            @click="navigate"
          >
            <div class="sub-nav-icon">
              <el-icon><component :is="item.icon" /></el-icon>
            </div>
            <div class="sub-nav-text">{{ item.name }}</div>
          </div>
        </router-link>
      </div> -->

      <!-- 诊断管理折叠菜单 -->
      <div
        class="nav-item collapsible"
        :class="{ active: isDiagnosisActive }"
        @click="toggleDiagnosisMenu"
      >
        <div class="nav-icon">
          <el-icon><FirstAidKit /></el-icon>
        </div>
        <div class="nav-text">诊断管理</div>
        <div class="nav-arrow" :class="{ rotated: diagnosisMenuOpen }">
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div>

      <!-- 诊断管理子菜单 -->
      <div class="sub-menu" :class="{ open: diagnosisMenuOpen }">
        <router-link
          v-for="item in diagnosisSubItems"
          :to="item.path"
          :key="item.key"
          custom
          v-slot="{ navigate, isActive }"
        >
          <div
            class="sub-nav-item"
            :class="{ active: isActive }"
            @click="navigate"
          >
            <div class="sub-nav-icon">
              <el-icon><component :is="item.icon" /></el-icon>
            </div>
            <div class="sub-nav-text">{{ item.name }}</div>
          </div>
        </router-link>
      </div>

      <!-- 其他菜单项 -->
      <router-link
        v-for="item in otherMenuItems"
        :to="item.path"
        :key="item.key"
        custom
        v-slot="{ navigate, isActive }"
      >
        <div class="nav-item" :class="{ active: isActive }" @click="navigate">
          <div class="nav-icon">
            <el-icon><component :is="item.icon" /></el-icon>
          </div>
          <div class="nav-text">{{ item.name }}</div>
          <div class="nav-badge" v-if="item.badge && item.badge > 0">
            {{ item.badge }}
          </div>
        </div>
      </router-link>
    </div>
    <!-- 用户信息及操作 -->
    <div class="user-profile">
      <el-dropdown trigger="click" @command="handleCommand">
        <div class="user-avatar">
          <el-icon><User /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="profile">个人资料</el-dropdown-item>
            <el-dropdown-item command="settings">账户设置</el-dropdown-item>
            <el-dropdown-item divided @click="logout"
              >注销登录</el-dropdown-item
            >
          </el-dropdown-menu>
        </template>
      </el-dropdown>

      <div class="user-info">
        <div class="user-name">{{ username }}</div>
        <div class="user-role">{{ userDepartment }}</div>
        <div class="user-status">
          <span class="status-dot" :class="userStatus"></span>
          {{ statusText }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useStore } from "vuex";
import { ElMessage } from "element-plus";

const sidebarOpen = ref(false);
const router = useRouter();
const route = useRoute();
const store = useStore();

// 菜单展开状态
const patientRecordMenuOpen = ref(false);
const medicalRecordMenuOpen = ref(false);
const diagnosisMenuOpen = ref(false);

// logo图标
const logoUrl = require("@/assets/logo/logo_h.png");

// 配置菜单项 - 折叠菜单之前的平铺菜单
const flatMenuItems = ref([
  {
    name: "工作台",
    path: "/doctor/home",
    icon: "House",
    key: "dashboard",
  },
  {
    name: "预约挂号管理",
    path: "/doctor/reservation",
    icon: "List",
    key: "reservation",
    badge: 0, // 待处理预约数量
  },
  { 
    name: "患者档案管理", 
    path: "/doctor/patient-list", 
    icon: "User", 
    key: "patient-list" 
  },
]);

// 患者病历管理子菜单项
const patientRecordSubItems = ref([
  
  // {
  //   name: "病历模板",
  //   path: "/doctor/medicalrecord-templates",
  //   icon: "Collection",
  //   key: "medicalrecord-templates",
  // },
]);

// 诊断管理子菜单项
const diagnosisSubItems = ref([
  {
    name: "诊断记录",
    path: "/doctor/diagnosis-list",
    icon: "Search",
    key: "diagnosis-list",
  },
  {
    name: "处方管理",
    path: "/doctor/prescription",
    icon: "Memo",
    key: "prescription",
  },
  {
    name: "检查开具",
    path: "/doctor/examination-orders",
    icon: "Reading",
    key: "lab-orders",
  },
  {
    name: "检查结果",
    path: "/doctor/examination-results",
    icon: "DocumentChecked",
    key: "lab-results",
  },
]);

// 折叠菜单之后的其他菜单项
const otherMenuItems = ref([
  {
    name: "我的排班",
    path: "/doctor/doctor-schedule",
    icon: "Calendar",
    key: "schedule",
    badge: 0, // 今日排班数量
  },
    
]);

// 模拟数据
const userStatus = ref("online"); // online, busy, offline

// 用户信息
const username = ref(store.state.user?.doctor_name || "张医生");
const userDepartment = ref(store.state.user?.department_name || "内科");

// 状态文本
const statusText = computed(() => {
  switch (userStatus.value) {
    case "online":
      return "在线";
    case "busy":
      return "忙碌";
    case "offline":
      return "离线";
    default:
      return "在线";
  }
});

// 检查当前路由是否在患者病历管理子菜单中
const isPatientRecordActive = computed(() => {
  return patientRecordSubItems.value.some((item) => route.path.startsWith(item.path));
});

// 切换患者病历管理菜单展开状态
const togglePatientRecordMenu = () => {
  patientRecordMenuOpen.value = !patientRecordMenuOpen.value;
};

// 检查当前路由是否在诊断管理子菜单中
const isDiagnosisActive = computed(() => {
  return diagnosisSubItems.value.some((item) =>
    route.path.startsWith(item.path)
  );
});

// 切换患者管理菜单展开状态
const togglePatientMenu = () => {
  patientMenuOpen.value = !patientMenuOpen.value;
};

// 切换病历管理菜单展开状态
const toggleMedicalRecordMenu = () => {
  medicalRecordMenuOpen.value = !medicalRecordMenuOpen.value;
};

// 切换诊断管理菜单展开状态
const toggleDiagnosisMenu = () => {
  diagnosisMenuOpen.value = !diagnosisMenuOpen.value;
};

// 初始化时，如果当前路由在对应子菜单中，自动展开
onMounted(() => {
  if (isPatientRecordActive.value) {
    patientRecordMenuOpen.value = true;
  }
  if (isDiagnosisActive.value) {
    diagnosisMenuOpen.value = true;
  }
});

const handleCommand = (command) => {
  switch (command) {
    case "profile":
      router.push("/doctor/profile");
      break;
    case "settings":
      router.push("/doctor/settings");
      break;
    default:
      break;
  }
};

const logout = () => {
  localStorage.clear();
  router.push("/login");
  ElMessage.success("已安全退出系统");
};
</script>

<style lang="scss" scoped>
//设置本地字体
@font-face {
  font-family: "MyBrushFont";
  src: url("@/assets/fonts/W04.ttf") format("truetype");
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

/* 侧边栏样式 */
.sidebar {
  width: 280px;
  background: linear-gradient(135deg, #1a75bc 0%, #0c4d8c 100%);
  color: white;
  padding: 20px 0;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  transition: width 0.3s ease;
}

.hospital-brand {
  display: flex;
  align-items: center;
  padding: 0 20px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  margin-bottom: 20px;
}

.hospital-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;

  img {
    width: 50px;
    height: 50px;
  }
}

.brand-text {
  font-family: "MyBrushFont", "ZCOOL XiaoWei", serif;
  font-size: 22px;
  cursor: pointer;
}

.nav-menu {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 14px 20px;
  cursor: pointer;
  transition: all 0.3s;
  position: relative;

  .nav-text {
    flex: 1;
    font-size: 15px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  &:hover {
    background: rgba(255, 255, 255, 0.1);
  }

  &.active {
    background: rgba(255, 255, 255, 0.2);
    border-left: 4px solid white;
  }

  &.collapsible {
    justify-content: space-between;
  }
}

.nav-icon {
  margin-right: 12px;
  width: 20px;
  text-align: center;
  flex-shrink: 0;
}

.nav-arrow {
  transition: transform 0.3s;
  flex-shrink: 0;

  &.rotated {
    transform: rotate(90deg);
  }
}

.nav-badge {
  background: #f56c6c;
  color: white;
  border-radius: 10px;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 500;
  min-width: 20px;
  text-align: center;
  flex-shrink: 0;
}

/* 子菜单样式 */
.sub-menu {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;

  &.open {
    max-height: 300px; /* 根据子项数量调整 */
  }
}

.sub-nav-item {
  display: flex;
  align-items: center;
  padding: 10px 20px 10px 50px;
  cursor: pointer;
  transition: all 0.3s;
  background: rgba(0, 0, 0, 0.1);

  &:hover {
    background: rgba(255, 255, 255, 0.05);
  }

  &.active {
    background: rgba(255, 255, 255, 0.15);
    border-left: 2px solid rgba(255, 255, 255, 0.5);
  }
}

.sub-nav-icon {
  margin-right: 10px;
  width: 16px;
  text-align: center;
  opacity: 0.8;
  flex-shrink: 0;
}

.sub-nav-text {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-profile {
  position: relative;
  padding: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 1);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  cursor: pointer;
  flex-shrink: 0;
  color: #1a75bc;
}

.user-info {
  flex: 1;
  min-width: 0;
}

.user-name {
  font-weight: 500;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-role {
  font-size: 12px;
  opacity: 0.8;
  margin-bottom: 2px;
}

.user-status {
  display: flex;
  align-items: center;
  font-size: 11px;
  opacity: 0.7;
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;

  &.online {
    background: #67c23a;
  }

  &.busy {
    background: #e6a23c;
  }

  &.offline {
    background: #909399;
  }
}

/* 响应式设计 */
@media (max-width: 992px) {
  .sidebar {
    width: 80px;
  }

  .brand-text,
  .nav-text,
  .user-info,
  .sub-nav-text,
  .nav-arrow,
  .nav-badge {
    display: none;
  }

  .hospital-brand {
    justify-content: center;
    padding: 15px 0;
  }

  .nav-item {
    justify-content: center;
    padding: 16px 0;

    &.collapsible {
      justify-content: center;
    }
  }

  .nav-icon {
    margin-right: 0;
  }

  .user-profile {
    justify-content: center;
  }

  .sub-nav-item {
    padding-left: 0;
    justify-content: center;
  }

  .sub-nav-icon {
    margin-right: 0;
  }
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: -80px;
    height: 100%;
    z-index: 1000;
    transition: left 0.3s;
  }

  .sidebar.open {
    left: 0;
  }

  .nav-item.collapsible {
    justify-content: center;
  }

  .menu-toggle {
    display: block !important;
  }
}

.menu-toggle {
  display: none;
  margin-right: 15px;
  cursor: pointer;
}

/* 滚动条样式 */
.nav-menu::-webkit-scrollbar {
  width: 4px;
}

.nav-menu::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
}

.nav-menu::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 2px;
}

.nav-menu::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.5);
}
</style>